﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.Mobile.PasswordAuthentication;

namespace PasswordAuthenticationGettingStarted
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private const string SecurityContainerName = "MyFirstSecureApplication";


        private void RegisterButton_Click(object sender, EventArgs e)
        {
            using (var cryptoProvider = new RsaAesCryptographyProvider(SecurityContainerName))
            {
                PasswordIdentity identity = new PasswordIdentity(this.UserNameText.Text,
                                                                this.PasswordText.Text,
                                                                cryptoProvider);
                this.RegisteredUserKey.Text = Convert.ToBase64String(identity.CryptoKey);
                this.RegisteredUserLabel.Text = this.UserNameText.Text + " is the registered user";
            }

            this.PasswordText.Text = "";
        }

        private void SignInButton_Click(object sender, EventArgs e)
        {
            using (var cryptoProvider = new RsaAesCryptographyProvider(SecurityContainerName))
            {
                PasswordIdentity identity = new PasswordIdentity(this.UserNameText.Text,
                                                                this.PasswordText.Text,
                                                                cryptoProvider);
                this.SignInUserLabel.Visible = true;
                this.SignInUserKey.Text = Convert.ToBase64String(identity.CryptoKey);


                if (this.RegisteredUserKey.Text == this.SignInUserKey.Text)
                {
                    this.AuthenticatedLabel.Text = "Correct Password";
                    this.AuthenticatedLabel.ForeColor = Color.Green;
                }else{
                    this.AuthenticatedLabel.Text = "Incorrect Password";
                    this.AuthenticatedLabel.ForeColor = Color.Red;
                }
                this.AuthenticatedLabel.Visible = true;
            }

        }

       

     
    }
}